#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

#来源：https://dzone.com/articles/using-gitlab-rest-api-to-create-projects
# API Documentation https://docs.gitlab.com/ee/api/api_resources.html
source ./env.gitlab

export GITLAB_SERVER=http://${GIT_HOST}
# http://localhost/-/profile/personal_access_tokens 处生成
export GITLAB_TOKEN=${1-"2NGDqoxP5vD-vxdey-5d"}
echo "your access token is: ${GITLAB_TOKEN}"
echo "PRJ: $GIT_HOST:$GITLAB_ROOTGROUP/$GITLAB_SUBGROUP/$GITLAB_PROJECT"

# export GITLAB_ROOTGROUP=qa
# export GITLAB_SUBGROUP=tca
# export GITLAB_PROJECT=demo
#export GITLAB_FEATURE=feature/my-feature-01

#
#creating root group
#
rootGroupId=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_SERVER/api/v4/groups?search=$GITLAB_ROOTGROUP" | jq '.[0]["id"]' )
if [ $rootGroupId == "null" ] 
then
  rootGroupId=$(curl -s -d "name=$GITLAB_ROOTGROUP&path=$GITLAB_ROOTGROUP&visibility=private&lfs_enabled=true&description=Root group" -X POST "$GITLAB_SERVER/api/v4/groups" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Root group Id: $rootGroupId"

#
#creating sub group
#
rootSubGroupId=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_SERVER/api/v4/groups/$rootGroupId/subgroups?search=$GITLAB_SUBGROUP" | jq '.[0]["id"]' )
if [ $rootSubGroupId == "null" ] 
then
  rootSubGroupId=$(curl -s -d "name=$GITLAB_SUBGROUP&path=$GITLAB_SUBGROUP&visibility=private&lfs_enabled=true&description=$GITLAB_SUBGROUP programme&parent_id=$rootGroupId" -X POST "$GITLAB_SERVER/api/v4/groups" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Sub group Id: $rootSubGroupId"

#
#project creation
#
projectId=$(curl -s "$GITLAB_SERVER/api/v4/groups/$rootSubGroupId/projects?search=$GITLAB_PROJECT" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.[0]["id"]' )
if [ $projectId == "null" ] 
then
  projectId=projectId=$(curl -s -d "path=$GITLAB_PROJECT&namespace_id=$rootSubGroupId" -X POST "$GITLAB_SERVER/api/v4/projects" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Project Id: $projectId"

#
#Clonning git project and generating basic java maven structure
# 
# mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=edu.emmerson.gitlab -DartifactId=$GITLAB_PROJECT -Dversion=0.0.1
# cd $GITLAB_PROJECT
# git init
# git remote add origin git@localhost:$GITLAB_ROOTGROUP/$GITLAB_SUBGROUP/$GITLAB_PROJECT.git
# git add .
# git commit -m "Initial commit"
# git push -u origin master

#
# Git branches initial structure as per gitflow
#
projectId=$(curl -s "$GITLAB_SERVER/api/v4/groups/$rootSubGroupId/projects?search=$GITLAB_PROJECT" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.[0]["id"]' )
echo "Project Id: $projectId"

# #create develop branch
# curl -s -d "branch=develop&ref=master" -X POST "$GITLAB_SERVER/api/v4/projects/$projectId/repository/branches" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq

# #create feature branch
# curl -s -d "branch=$GITLAB_FEATURE&ref=develop" -X POST "$GITLAB_SERVER/api/v4/projects/$projectId/repository/branches" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq

#删除xiangmu
#curl -s -X DELETE "$GITLAB_SERVER/api/v4/projects/$projectId" -H "PRIVATE-TOKEN: $GITLAB_TOKEN"
#Reset Gitlab Project Repository Content
#git push -f master origin .

./api-init.sh || true
